package cn.ssms.interceptor;


import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;



/**
 * Controller 拦截  
 * @author ll
 *
 */
@Component
@Aspect
public class AspectControllerFilter {
	public AspectControllerFilter(){}
	
	//前置通知
    @Before(value="execution(* cn.ssms.controller.*.*(..))")
    public void doBefore(JoinPoint jp){
//    	System.out.println(jp.getSignature().getName()+"=====");
//    	String method = jp.getSignature().getName();
//    	HttpServletRequest request = null;
//    	Map map = new HashMap();
//    	Gson gs = new Gson();
//        Object[] obj = jp.getArgs();
//        for(int i=0;i<obj.length;i++){
//        	 if (obj[i] instanceof HttpServletRequest) {  
//                 request = (HttpServletRequest) obj[i];
//                 String userid = String.valueOf(request.getSession().getAttribute("userid"));
//                 String username = String.valueOf(request.getSession().getAttribute("username"));
//               
//                 String ip = TextUtil.getIpAddr(request);
//                 String region = AddressUtils.getRegion(ip);
//	             
//                 
//                 map = GetRequestParam.setMap(request);
//                 Log log = new Log();
//                 log.setUser_id(userid);
//                 log.setName(username);
//                 log.setMethod(method);
//                 log.setIp(ip);
//                 log.setRegion(region);
//                 log.setType(0);
//                 log.setContent(gs.toJson(map));
//                 commonService.writeOperationLog(log);
//             }
//        }
//        System.out.println("执行了前置通知");
    }
     
    //后置通知
    @After(value="execution(* cn.ssms.controller.*.*(..))")
    public void doAfter(){
//        System.out.println("执行了后置通知");
    	
    }
     
    //环绕通知
    @Around(value="execution(* cn.ssms.controller.*.*(..))")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
        Object retVal = pjp.proceed();
        return retVal;
    }
     
    //获取返回后通知
    @AfterReturning(value="execution(* cn.ssms.controller.*.*(..))",returning="retVal")
    public void doAfterReturning(JoinPoint jp,String retVal){
        System.out.println("后置返回值通知，获得参数："+retVal);
    }
     
    //异常通知
    @AfterThrowing(value="execution(* cn.ssms.controller.*.*(..))",throwing="ex")
    public void afterThrowing(JoinPoint jp,Throwable ex){
        System.out.println("抛出异常通知"+ex+"==");
    }
}
